﻿/*****************************************介绍*****************************************
 * 作者：24531
 * 创建时间：2025-09-07 18:48:00
 * 功能：界面【NewJourenyPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.NewJourenyPanel
{
    public class NewJourenyPanelView : PopupPanel
    {
        public const string PANEL_KEY = "Game_UI_NewJourenyPanel_NewJourenyPanel_Key";
        private GameDataModel.Level m_Level;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
            EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, CloseSuccessAd);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
            EventCenter.Instance.RemoveEventListen<EAdType, ETriggerAdType>(GameEventName.ON_CLOSE_AD, CloseSuccessAd);
        }

        public override void Init(object data)
        {
            var datas = data as object[];
            m_Level = datas[0] as GameDataModel.Level;
            UpdateDownloadBtn();

            var bg = datas[1] as Sprite;
            var icon = GetControl<Image>(NewJourenyPanelNotifications.Icon_CTRL);
            icon.sprite = bg;
            var size = UIUtility.CalcSizeToFill(icon.transform.parent.GetComponent<RectTransform>(), bg.texture);
            icon.rectTransform.sizeDelta = new Vector2(size.width, size.height);
        }

        private bool UpdateDownloadBtn()
        {
            var cfg = m_Level.level <= BaseDataMgr.Instance.Cfg.level.level_data.Length ? BaseDataMgr.Instance.Cfg.level.level_data[m_Level.level - 1] : BaseDataMgr.Instance.Cfg.level.other_level;
            bool isFreeDownload = m_Level.downloadAdCount >= cfg.download_ads;
            GetRect(NewJourenyPanelNotifications.Ad_RECT).gameObject.SetActive(!isFreeDownload);
            var label = BaseDataMgr.Instance.GetLanguageValue("Download");
            GetControl<Text>(NewJourenyPanelNotifications.Label_CTRL).text = isFreeDownload ? label : $"[{m_Level.downloadAdCount}/{cfg.download_ads}] {label}";
            return isFreeDownload;
        }

        private void CloseSuccessAd(EAdType adType, ETriggerAdType triggerAdType)
        {
            if (adType != EAdType.RewardedVideo || triggerAdType != ETriggerAdType.DownloadImg) return;
            m_Level.downloadAdCount++;
            bool isFreeDownload = UpdateDownloadBtn();
            if (isFreeDownload)
            {
                string path = m_Level.level == 1 || m_Level.finalSelectIdx == -1 ?
                              AssetCacheServer.Instance.DefaultBgPath :
                              AssetCacheServer.Instance.GetAssetPath(m_Level.assetsInfo[m_Level.finalSelectIdx].type, m_Level.assetsInfo[m_Level.finalSelectIdx].id);
                NativeInteraction.Instance.SaveImageToAlbum(path);
                UIUtility.HidePanel(PANEL_KEY);
            }
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case NewJourenyPanelNotifications.DownloadBtn_CTRL:
                    NativeInteraction.Instance.ShowRewardAd(ETriggerAdType.DownloadImg);
                    break;
                case NewJourenyPanelNotifications.ContinueBtn_CTRL:
                    UIUtility.HidePanel(PANEL_KEY);
                    break;
            }
        }
    }
}